package net.blank.ui.core
{
	import flash.display.BitmapData;
	import flash.geom.Rectangle;
	
	import net.blank.bitmapOperation.BitmapDraw;
	import net.blank.commonInterface.IClone;
	import net.blank.commonInterface.IDestructible;
	import net.blank.functionUtl.FunctionBinder;

	
	/**
	 * 皮肤
	 * @author blank
	 * 
	 */
	public class Skin implements IDestructible,IClone
	{
		/**
		 * 已经被摧毁
		 */
		private var _destroyed:Boolean;
		
		/**
		 * 背景图像
		 */
		protected var _background:BitmapData;
		
		/**
		 * 进行9格缩放时，缩放的内部区域
		 */
		protected var _innerScaleRect:Rectangle;
		
		/**
		 * 禁用交互性事件时的滤镜
		 */
		protected var _disabledFilters:Array;
		
		/**
		 * 9格缩放的切片缓存
		 */
		protected var _clipCache:Object;
		
		/**
		 * 皮肤
		 * @param backGround 背景图像
		 * @param innerScaleRect 九格缩放时，缩放的内部区域
		 * @param disabledFilters 禁用交互性事件时的滤镜
		 * 
		 */
		public function Skin(
			background:BitmapData=null,
			innerScaleRect:Rectangle=null,
			disabledFilters:Array=null
		){
			_background=background;
			_innerScaleRect=innerScaleRect;
			_disabledFilters=disabledFilters;
		}
		
		/**
		 * 背景图像(可绑定)
		 * @param value
		 * 
		 */
		public function setBackground(value:BitmapData):void{
			if(value != _background){
				_background=value;
				
				_clipCache=null;
				
				FunctionBinder.execCallbackFun(setBackground,[this]);
			}
		}
		/**
		 * 背景图像
		 * @return 
		 * 
		 */
		public function getBackground():BitmapData{
			return _background;
		}
		
		/**
		 * 九格缩放时，缩放的内部区域(可绑定)
		 * @param value
		 * 
		 */
		public function setInnerScaleRect(value:Rectangle):void{
			if(value != _innerScaleRect){
				_innerScaleRect=value;
				
				_clipCache=null;
				
				FunctionBinder.execCallbackFun(setInnerScaleRect,[this]);
			}
		}
		/**
		 * 九格缩放时，缩放的内部区域
		 * @return 
		 * 
		 */
		public function getInnerScaleRect():Rectangle{
			return _innerScaleRect;
		}
		
		/**
		 * 禁用交互性事件时的滤镜(可绑定)
		 * @param value
		 * 
		 */
		public function setDisabledFilters(value:Array):void{
			if(value != _disabledFilters){
				_disabledFilters=value;
				
				FunctionBinder.execCallbackFun(setDisabledFilters,[this]);
			}
		}
		/**
		 * 禁用交互性事件时的滤镜
		 * @return 
		 * 
		 */
		public function getDisabledFilters():Array{
			return _disabledFilters;
		}
		
		/**
		 * 9格缩放时的切片缓存
		 * @return 
		 * 
		 */
		public function get clipCache():Object{
			if(_clipCache){
				return _clipCache;
			}
			
			if(_background){
				if(_innerScaleRect){
					_clipCache=BitmapDraw.sliceBitmapForScale9Gride(_background,_innerScaleRect);
				}
			}
			
			return _clipCache;
		}
		
		public function get destroyed():Boolean{
			return _destroyed;
		}
		
		public function destroy():void{
			if(_background){
				_background.dispose();
				_background=null;
			}
			_innerScaleRect=null;
			_disabledFilters=null;
			
			_clipCache=null;
			
			FunctionBinder.clearBindingFuns(
				setBackground,
				setInnerScaleRect,
				setDisabledFilters
			);
			
			_destroyed=true;
		}
		
		public function clone():*{
			return new Skin(
				_background ? _background.clone() : null,
				_innerScaleRect ? _innerScaleRect.clone() : null,
				_disabledFilters ? cloneFilters(_disabledFilters) : null
			);
		}
		
		/**
		 * 复制滤镜数组
		 * @param filters
		 * @return 
		 * 
		 */
		public static function cloneFilters(filters:Array):Array{
			var newFilters:Array=new Array();
			var len:int=filters.length;
			for(var i:int=0;i<len;i++){
				newFilters[i]=filters[i].clone();
			}
			
			return newFilters;
		}
	}
}